const lineChart = (xData, names, datas, colors, tip = true) => {
  const series = []
  for (const i in datas) {
    series.push({
      name: names[i],
      type: 'line',
      data: datas[i],
      itemStyle: {
        color: colors[i]
      }
    })
  }
  return {
    tooltip: {
      trigger: 'axis',
      triggerOn: 'mousemove|click',
      formatter: params => {
        let str = params[0].name + '峰平谷用电量<br>'
        for (const item of params) {
          str += item.marker
          str += item.seriesName + ':' + item.data + '度<br>'
        }
        return str
      },
      show: tip
    },
    legend: {
      data: names,
      top: '30'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
    },
    yAxis: {
      type: 'value'
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '3%',
      containLabel: true
    },
    series
  }
}

const SimpleLineChart = (xData, datas) => {
  return {
    xAxis: {
      type: 'category',
      data: xData,
      axisLine: {
        lineStyle: {
          color: '#b3b3b3'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLine: { show: false },
      axisLabel: { show: false },
      axisTick: { show: false },
      splitLine: { show: false }
    },
    series: [{
      data: datas,
      type: 'line',
      areaStyle: {}
    }],
    grid: {
      top: '0',
      left: '0',
      right: '8',
      bottom: '0%',
      containLabel: true
    }
  }
}

const LinearColor = (opt, echarts) => {
  for (const item in opt.series) {
    opt.series[item].itemStyle = {
      color: new echarts.graphic.LinearGradient(
        0, 0, 0, 1,
        [
          {
            offset: 0,
            color: '#0095ff'
          },
          {
            offset: 1,
            color: '#ffffff'
          }
        ]
      )
    }
  }
  return opt
}

const BriefPie = (names, datas, colors) => {
  const data = []
  for (const i in names) {
    data.push({
      value: datas[i],
      name: names[i],
      itemStyle: {
        color: colors[i]
      }
    })
  }
  return {
    tooltip: {
      trigger: 'item'
    },
    series: [
      {
        type: 'pie',
        radius: '70%',
        center: ['50%', '55%'],
        data: data
      }
    ]
  }
}

const VerticalChart = (xData, names, datas) => {
  const seriesData = []
  for (const i in names) {
    seriesData.push({
      name: name[i],
      type: 'bar',
      data: datas[i],
      label: {
        show: true,
        position: 'right'
      }
    })
  }
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: params => {
        return params[0].name + '电费' + params[0].data + '元'
      }
    },
    legend: {
      data: names,
      top: '30'
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: xData
    },
    grid: {
      top: '20',
      left: '3%',
      right: '10%',
      bottom: '3%',
      containLabel: true
    },
    series: seriesData
  }
}

const VerticalDoubleChart = (xData, names, datas, colors) => {
  const seriesData = []
  for (const i in datas) {
    seriesData.push({
      name: names[i],
      type: 'bar',
      label: {
        show: true,
        position: 'right'
      },
      data: datas[i],
      itemStyle: {
        color: colors[i]
      }
    })
  }
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: params => {
        let str = params[0].name + '<br>'
        str += params[0].marker + params[0].seriesName + ':' + params[0].data + '度<br>'
        if (params.length > 1) {
          str += params[1].marker + params[1].seriesName + ':' + params[1].data + '元'
        }
        return str
      }
    },
    legend: {
      data: names,
      top: '10'
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: xData
    },
    grid: {
      top: '50',
      left: '3%',
      right: '10%',
      bottom: '3%',
      containLabel: true
    },
    series: seriesData
  }
}

const months = (n) => {
  const mons = []
  for (let i = 1; i <= n; i++) {
    mons.push(i + '月')
  }
  return mons
}

const numbers = (n) => {
  const num = []
  for (let i = 1; i <= n; i++) {
    num.push(i)
  }
  return num
}

export default {
  lineChart,
  SimpleLineChart,
  BriefPie,
  VerticalChart,
  VerticalDoubleChart,
  months,
  numbers,
  LinearColor
}
